package cn.doitedu.day04

object FunctionDemo {

  def main(args: Array[String]): Unit = {

    val f1 = (x: Int, y: Double) => (y, x)
    val f2: (Int, Double) => (Double, Int) = (x: Int, y: Double) => (y, x)

    //函数到底是什么？，函数是引用类型

    //在scala中，所有的函数都是FunctionN（0~22）的实例，即特质的实例
    //FunctionN，N代表函数输入参数的格式
    //new Function2并且重新apply方法，相当于new 了一个特质的实现
    val f3 = new Function2[Int, Double, Double] {
      override def apply(v1: Int, v2: Double): Double = {
        v1 + v2
      }
    }
    val r = f3(5, 6.6)
    val r2 = f3.apply(5, 6.6)
    println(r2)

    val f4 = new Function2[Int, Double, (Double, Int)] {
      override def apply(v1: Int, v2: Double): (Double, Int) = {
        (v2, v1)
      }
    }
    val r3: (Double, Int) = f4.apply(5, 6.6)
    println(r3)


    val f5 = new Function1[Int, Int] {
      override def apply(v1: Int): Int = {
        v1 * 10
      }
    }
    val r4 = f5(6)
    println(r4)


    val f6: Function1[Int, Int] = (x: Int) => x * 10
    val r5 = f6(8)
    println(r5)


    val f7 = new ((Int) => Int) {
      override def apply(v1: Int): Int = {
        v1 * 10
      }
    }
    val r6 = f7(9)
    println(r6)


    val f8: (Int) => Int = (x: Int) => x * 10
    val r7 = f6(8)
    println(r7)



  }

}
